package com.unibuy.smartdevice.devices;

import java.util.ArrayList;
import java.util.List;

/*
    9600, 8, 1, NONE/EVEN
    Checking device: /dev/bus/usb/004/007 開發板
    Vendor ID: 8746
    Product ID: 1
    Checking device: /dev/bus/usb/001/002 螢幕線
    Vendor ID: 7694
    Product ID: 36865
    Checking device: /dev/bus/usb/004/011 掃碼頭
    Vendor ID: 4619
    Product ID: 33286
*/
public enum SerialPort {
    IDENTIFICATION(0, 65535, 9600, 8, 1, "NONE", DeviceType.IDENTIFICATION),
    UNKNOWN1(0, 65535, 9600, 8, 1, "NONE", DeviceType.UNKNOWN),
    UNKNOWN2(1, 65535, 9600, 8, 1, "NONE", DeviceType.UNKNOWN),
    UNKNOWN3(2, 65535, 9600, 8, 1, "NONE", DeviceType.UNKNOWN),
    STATIC5(3034, 33106, 9600, 8, 1, "NONE", DeviceType.UNKNOWN),
    STATIC1(6791, 29987, 9600, 8, 1, "NONE", DeviceType.UNKNOWN),
    STATIC2(3034, 61817, 9600, 8, 1, "NONE", DeviceType.UNKNOWN),
    STATIC3(8746, 1, 9600, 8, 1, "NONE", DeviceType.UNKNOWN),
    STATIC4(7694, 36865, 9600, 8, 1, "NONE", DeviceType.UNKNOWN),
    QRCODE_SCANNER1(4619, 33286, 9600, 8, 1, "NONE", DeviceType.QRCODE_SCANNER),
    QRCODE_SCANNER2(5418, 34831, 9600, 8, 1, "NONE", DeviceType.QRCODE_SCANNER),
    QRCODE_SCANNER3(1317, 42151, 9600, 8, 1, "NONE", DeviceType.QRCODE_SCANNER),
    QRCODE_SCANNER4(1317, 42156, 9600, 8, 1, "NONE", DeviceType.QRCODE_SCANNER),
    ;

    private final int vendorID;
    private final int productID;
    private final int baudRate;
    private final int dataBits;
    private final int stopBits;
    private final String parity;
    private final DeviceType deviceType;

    // 靜態篩選方法
    public static List<SerialPort> getPortsByDeviceType(DeviceType type) {
        List<SerialPort> result = new ArrayList<>();
        for (SerialPort port : SerialPort.values()) {
            if (port.getDeviceType() == type) {
                result.add(port);
            }
        }
        return result;
    }
    public static SerialPort getPortsByVendorInfo(int vendorID, int productID) {
        for (SerialPort serialPort : SerialPort.values()) {
            if (serialPort.getVendorID() == vendorID && serialPort.getProductID() == productID) {
                return serialPort;
            }
        }
        return SerialPort.IDENTIFICATION;
    }
    SerialPort(int vendorID, int productID, int baudRate, int dataBits, int stopBits, String parity, DeviceType deviceType) {
        this.vendorID = vendorID;
        this.productID = productID;
        this.baudRate = baudRate;
        this.dataBits = dataBits;
        this.stopBits = stopBits;
        this.parity = parity;
        this.deviceType = deviceType;
    }

    public int getVendorID() {
        return vendorID;
    }

    public int getProductID() {
        return productID;
    }

    public int getBaudRate() {
        return baudRate;
    }

    public int getDataBits() {
        return dataBits;
    }

    public int getStopBits() {
        return stopBits;
    }

    public String getParity() {
        return parity;
    }

    public DeviceType getDeviceType() {
        return deviceType;
    }

    @Override
    public String toString() {
        return "SerialPort{" +
                "vendorID='" + vendorID +
                ", productID=" + productID +
                ", baudRate=" + baudRate +
                ", dataBits=" + dataBits +
                ", stopBits=" + stopBits +
                ", parity='" + parity +
                ", deviceType=" + deviceType.getDescription() +
                '}';
    }
}
